package light.mvc.service.patient.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.admin.Tuser;
import light.mvc.model.item.Ttreatment;
import light.mvc.model.register.Tregister;
import light.mvc.model.register.TregisterTreatment;
import light.mvc.page.base.PageFilter;
import light.mvc.page.patient.DoctorRegistration;
import light.mvc.service.patient.SiteRegisterServiceI;
import light.mvc.utils.GlobalConstant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SiteRegisterServiceImpl implements SiteRegisterServiceI {

	@Autowired
	private BaseDaoI<Tuser> userDao;
	
	@Autowired
	private BaseDaoI<Tregister> registerDao;
	
	@Autowired 
	private BaseDaoI<TregisterTreatment> registerTreatmentDao;
	
	@Autowired
	private BaseDaoI<Ttreatment> treatmentDao;
	
	@Override
	public List<DoctorRegistration> doctorGrid(DoctorRegistration someDoctorRegistration, Date timeSegment,PageFilter ph){
		List<DoctorRegistration> doctorRegistrations = new ArrayList<DoctorRegistration>();
		Map<String, Object> params = new HashMap<String, Object>();
		
		String hql = "select t from Tuser t join t.role r";
		if(someDoctorRegistration.getDepartmentId() != null) {
			params.put("departmentId", someDoctorRegistration.getDepartmentId());
			hql += " join t.department d where d.id = :departmentId and r.id = 4";
		} else {
			hql += " where r.id = 4";
		}
		
		List<Tuser> list = userDao.find(hql + orderHql(ph), params, ph.getPage(), ph.getRows());
		
		for (Tuser t : list) {
			DoctorRegistration doctorRegistration = new DoctorRegistration();
			doctorRegistration.setDoctorId(t.getId());
			doctorRegistration.setDoctorName(t.getName());
			doctorRegistration.setDepartmentId(t.getDepartment().getId());
			doctorRegistration.setDepartmentName(t.getDepartment().getName());
			
			params = new HashMap<String, Object>();
			params.put("timeSegment", timeSegment);
			params.put("doctorId", t.getId());
			
			hql = "select count(*) from Tregister t join t.doctor doctor where t.type = 1 and doctor.id = :doctorId and t.timeSegment = :timeSegment";
			Long registeredNum = registerDao.count(hql,params);
			doctorRegistration.setSiteRegisteredNum(registeredNum);
			doctorRegistration.setSiteRegisterFreeNum(GlobalConstant.MAX_SITE_REGISTED-registeredNum);
			
			doctorRegistrations.add(doctorRegistration);
		}
		return doctorRegistrations;
	}
	
	
	
	@Override
	public Long doctorGridCount(DoctorRegistration someDoctorRegistration, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		
		String hql = " from Tuser t join t.role role";
		if(someDoctorRegistration.getDepartmentId() != null) {
			params.put("departmentId", someDoctorRegistration.getDepartmentId());
			hql += " join t.department department where department.id = :departmentId and role.id = 4";
		} else {
			hql += " where role.id = 4";
		}
		
		return userDao.count("select count(*) " + hql + orderHql(ph), params);
	}
	
	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	public void add(Integer doctorId,Integer patientId,String timeSegmentStr) throws Exception{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date timeSegment = format.parse(timeSegmentStr);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("doctorId", doctorId);
		params.put("timeSegment", timeSegment);
		
		String hql = "select count(*) from Tregister register join register.doctor doctor " +
		" where doctor.id = :doctorId and register.type = 1 and register.timeSegment = :timeSegment";
		Long num = registerDao.count(hql, params);
		if(num>=GlobalConstant.MAX_SITE_REGISTED) {
			throw new Exception("该医生的号已挂完");
		}
		
		hql = "select max(register.seq) from Tregister register join register.doctor doctor " +
		" where doctor.id = :doctorId and register.timeSegment = :timeSegment";
		Integer seq = registerDao.countInteger(hql, params);
		if(seq == null) {
			seq = 1;
		} else {
			seq++;
		}
		
		Tregister t = new Tregister();
		t.setType(1);
		t.setRegisterTime(new Date());
		t.setTimeSegment(timeSegment);
		t.setPatient(userDao.get(Tuser.class, patientId));
		t.setDoctor(userDao.get(Tuser.class, doctorId));
		t.setSeq(seq);
		
		registerDao.save(t);
		
		TregisterTreatment registerTreatment = new TregisterTreatment();
		registerTreatment.setRegister(t);
		registerTreatment.setTreatment(treatmentDao.get(Ttreatment.class, 1));
		registerTreatment.setChargeTime(new Date());
		registerTreatmentDao.save(registerTreatment);
	}
}
